package com.fancy.application.sys.permission.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fancy.application.common.service.impl.BaseService;
import com.fancy.application.common.utils.UserUtil;
import com.fancy.application.sys.org.entity.SysOrgModel;
import com.fancy.application.sys.org.entity.SysOrgPerson;
import com.fancy.application.sys.org.service.ISysOrgCoreService;
import com.fancy.application.sys.permission.entity.SysPermissionRes;
import com.fancy.application.sys.permission.entity.SysPermissionRole;
import com.fancy.application.sys.permission.entity.SysPermissionRoleOrg;
import com.fancy.application.sys.permission.mapper.SysPermissionRoleMapper;
import com.fancy.application.sys.permission.service.ISysPermissionResService;
import com.fancy.application.sys.permission.service.ISysPermissionRoleOrgService;
import com.fancy.application.sys.permission.service.ISysPermissionRoleResRelService;
import com.fancy.application.sys.permission.service.ISysPermissionRoleService;
import com.fancy.application.sys.permission.vo.SysPermissionRoleVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* fancycode自动生成v1.0
* @author wison
* 2019-09-30
*/

@Service("sysPermissionRoleService")
public class SysPermissionRoleServiceImpl extends BaseService<SysPermissionRoleMapper,SysPermissionRole> implements ISysPermissionRoleService {

    @Resource
    private ISysPermissionRoleResRelService sysPermissionRoleResRelService;
    @Resource
    private ISysPermissionResService sysPermissionResService;
    @Resource
    private ISysPermissionRoleOrgService sysPermissionRoleOrgService;
    @Resource
    private ISysOrgCoreService sysOrgCoreService;

    @Override
    public List<String> getResByUser(SysOrgPerson person) {
        if(UserUtil.isAdmin(person)){
            return sysPermissionResService.list().stream().map(SysPermissionRes::getFdResName).collect(Collectors.toList());
        }else{
            String personId = person.getFdId();
            return sysPermissionRoleResRelService.findResByPersonId(personId);
        }

    }

    @Override
    @Transactional
    public void saveOrUpdateRole(SysPermissionRoleVo sysPermissionRoleVo) {
        SysPermissionRole sysPermissionRole = new SysPermissionRole();
        BeanUtil.copyProperties(sysPermissionRoleVo,sysPermissionRole, CopyOptions.create().ignoreNullValue());
        this.saveOrUpdate(sysPermissionRole);
        //维护权限
        if (StrUtil.isNotEmpty(sysPermissionRoleVo.getCheckedRes())) {
            List<String> resList = StrUtil.splitTrim(sysPermissionRoleVo.getCheckedRes(),";");
            String fdRoleId = sysPermissionRole.getFdId();
            sysPermissionRoleResRelService.removeByRoleId(fdRoleId);
            resList.forEach(s -> sysPermissionRoleResRelService.saveRoleResRelation(s,fdRoleId));
        }
        //维护组织架构
        sysPermissionRoleOrgService.saveOrgIds(sysPermissionRole.getFdId(),sysPermissionRoleVo.getFdOrgId());
    }

    @Override
    @Transactional
    public void deleteSysPermissionRoleById(String id) {
        this.removeById(id);
        sysPermissionRoleOrgService.removeByRoleId(id);
    }

    /**
     * 加载指派的用户对象
     * @param sysPermissionRoleVo
     * @param id
     */
    @Override
    public void setOrgs(SysPermissionRoleVo sysPermissionRoleVo, String fdRoleId) {
        QueryWrapper<SysPermissionRoleOrg> orgQueryWrapper = new QueryWrapper<>();
        orgQueryWrapper.eq("fd_role_id",fdRoleId);
        List<SysPermissionRoleOrg> orgList = sysPermissionRoleOrgService.list(orgQueryWrapper);
        List<String> orgIds = orgList.stream().map(SysPermissionRoleOrg::getFdOrgId).collect(Collectors.toList());
        List<SysOrgModel> orgModels = sysOrgCoreService.getSysOrgModelList(orgIds);
        List<String> orgNames = orgModels.stream().map(SysOrgModel::getFdName).collect(Collectors.toList());
        sysPermissionRoleVo.setFdOrgId(CollectionUtil.join(orgIds,";"));
        sysPermissionRoleVo.setFdOrgName(CollectionUtil.join(orgNames,";"));
    }
}
